//
//  String+Shortcuts.swift
//  Coloring Tool
//
//  Created by Cedric Zheng on 21/11/2016.
//  Copyright © 2016 Cedric Zheng. All rights reserved.
//

import Foundation

extension String {
    
    /// Replace target string in this string with a new string
    ///
    /// - Parameters:
    ///   - target: the string that will be replaced
    ///   - withString: a new string that that will replace target
    /// - Returns: a string
    func replace(target: String, withString: String) -> String {
        return self.replacingOccurrences(of: target, with: withString)
    }
    
    func subString(fromEndOf firstStr: String, toBeginOf secondStr: String) -> String {
        guard
            self.characters.count > 0,
            let startPoint  = self.range(of: firstStr)?.upperBound,
            let endPoint    = self.range(of: secondStr)?.lowerBound
            else {
                return ""
        }
        
        let range = startPoint..<endPoint
        return self.substring(with: range)
    }
    
    
}
